package com.weather.androidweather.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.weather.androidweather.R;
import com.weather.androidweather.model.DayForecast;
import java.util.ArrayList;
import java.util.List;

public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ForecastViewHolder> {
    private List<DayForecast> forecasts = new ArrayList<>();

    public void setForecasts(List<DayForecast> forecasts) {
        this.forecasts = forecasts;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ForecastViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_forecast, parent, false);
        return new ForecastViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ForecastViewHolder holder, int position) {
        DayForecast forecast = forecasts.get(position);
        holder.bind(forecast);
    }

    @Override
    public int getItemCount() {
        return forecasts.size();
    }

    static class ForecastViewHolder extends RecyclerView.ViewHolder {
        TextView dateText;
        TextView weatherText;
        TextView temperatureText;
        ImageView weatherIcon;
        TextView windText;
        TextView humidityText;

        public ForecastViewHolder(@NonNull View itemView) {
            super(itemView);
            dateText = itemView.findViewById(R.id.dateText);
            weatherText = itemView.findViewById(R.id.weatherText);
            temperatureText = itemView.findViewById(R.id.temperatureText);
            weatherIcon = itemView.findViewById(R.id.weatherIcon);
            windText = itemView.findViewById(R.id.windText);
            humidityText = itemView.findViewById(R.id.humidityText);
        }

        void bind(DayForecast forecast) {
            dateText.setText(forecast.getFormattedDate());
            weatherText.setText(forecast.getWeather());
            temperatureText.setText(String.format("%d°/%d°", 
                forecast.getMaxTemp(), forecast.getMinTemp()));
            weatherIcon.setImageResource(forecast.getWeatherIconRes());
            windText.setText(String.format("%s %s级", 
                forecast.getWindDir(), forecast.getWindScale()));
            humidityText.setText(String.format("湿度 %d%%", forecast.getHumidity()));
        }
    }
} 